<?php

namespace catchAdmin\hunqing\model;

use catchAdmin\sq\model\Factory;
use catcher\base\CatchModel as Model;
use DateInterval;
use DateTime;
use think\facade\Db;
use think\Paginator;

/**
 *
 * @property int $id
 * @property string $name
 * @property int $sex
 * @property string $shengri
 * @property string $shuxiang
 * @property string $shengao
 * @property string $tizhong
 * @property string $xueli
 * @property string $minzu
 * @property int $che
 * @property int $chedai
 * @property int $fang
 * @property int $fangdai
 * @property string $shouru
 * @property string $fumu
 * @property string $jiemei
 * @property string $yaoqiu
 * @property string $laoshi
 * @property int $vip
 * @property string $lianxi
 * @property string $image
 * @property string $viptime
 * @property string $bak
 * @property int $created_at
 * @property int $updated_at
 * @property int $deleted_at
 * @property int $creator_id
 */
class Userinfos extends Model
{

    public $field = [
        //
        'id',
        //
        'name',
        //
        'sex',
        //
        'shengri',
        //
        'shuxiang',
        //
        'shengao',
        //
        'tizhong',
        //
        'xueli',
        //
        'minzu',
        //
        'che',
        //
        'chedai',
        //
        'fang',
        //
        'fangdai',
        //
        'shouru',
        //
        'fumu',
        'zinv',
        //
        'jiemei',
        //
        'yaoqiu',
        //
        'laoshi',
        //
        'vip',
        //
        'lianxi',
        'gongzuo',
        //
        'image',
        'image1',
        'image2',
        'image3',
        'image4',
        //
        'viptime',
        'hunyin',
        'vipdate',
        'yuegong',
        'xingqu',
        'baoxian',
        //
        'bak',
        // 创建时间
        'created_at',
        // 更新时间
        'updated_at',
        // 软删除字段
        'deleted_at',
        // 创建人ID
        'creator_id',
    ];

    public $name = 'userinfos';

    public function getList($id = null)
    {
        $data = $this->catchSearch()
            ->field('*')
            ->catchOrder()
            ->creator()
            ->paginate()->toarray();

        foreach ($data['data'] as &$v) {

            if ($id != 1) {
                $v['lianxi'] = $id == $v['creator_id'] ? $v['lianxi'] : '***';
                $v['b'] = $id == $v['creator_id'] ? 0 : 1;
                $v['e'] = $id == $v['creator_id'] ? 0 : 1;
                $v['f'] = $id == $v['creator_id'] ? 0 : 1;

            }

            $v['viptime'] = $this->checkExpirationStatus($v['vipdate'],$v['id']);
        }
        return Paginator::make($data['data'], $data['per_page'], $data['current_page'], $data['total']);
    }

    public function searchNameAttr($query, $value, $data)
    {
        $query->where('name', 'like', '%' . $value . '%');
    }

    public function searchLaoshiAttr($query, $value, $data)
    {
        $query->where('laoshi', 'like', '%' . $value . '%');
    }

    public function searchChushengAttr($query, $value, $data)
    {
        $query->where('shengri', 'like', '%' . $value . '%');
    }

    public function searchLianxiAttr($query, $value, $data)
    {
        $query->where('lianxi', 'like', '%' . $value . '%');
    }

    public function searchVipAttr($query, $value, $data)
    {
        $query->where('vip', $value);
    }

    public function searchGxAttr($query, $value, $data)
    {
        if ($value == 1) {
            $query->whereTime('updated_at', '<', '-30 days');
        }

    }

    public function searchhunyinAttr($query, $value, $data)
    {
        $query->where('hunyin', $value);

    }

    public function getSexAttr($value)
    {
        $status = [0 => '女', 1 => '男'];
        return $status[$value];
    }

    public function searchSexAttr($query, $value, $data)
    {
        $query->where('sex', $value);
    }


    public function checkExpirationStatus($date,$id) {
        // 检查是否为空（非会员）
        if (empty($date) || $date == '') {
            Db::name('userinfos')->where('id',$id)->update(['vip'=>2]);
            return "非会员";
        }

        // 验证日期格式
        if (!strtotime($date)) {
            return "无效的日期格式";
        }

        $currentDate = new DateTime('today');
        $membershipDate = new DateTime($date);

        // 计算加23个月后的日期
        $expiryDate23 = clone $membershipDate;
        $expiryDate23->add(new DateInterval('P23M'));

        // 计算加24个月后的日期
        $expiryDate24 = clone $membershipDate;
        $expiryDate24->add(new DateInterval('P24M'));

        // 判断状态
        if ($currentDate >= $expiryDate24) {
            Db::name('userinfos')->where('id',$id)->update(['vip'=>2]);
            return "会员时间已经过期";
        } elseif ($currentDate >= $expiryDate23) {
            Db::name('userinfos')->where('id',$id)->update(['vip'=>1]);
            return "会员时间即将到期";
        } else {
            Db::name('userinfos')->where('id',$id)->update(['vip'=>1]);
            return "超过一个月";
        }
    }

}